package com.usian.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.*;
import com.usian.pojo.*;
import com.usian.utils.IDUtils;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemService {

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    TbItemCatMapper itemCatMapper;
    @Autowired
    TbItemParamMapper itemParamMapper;
    @Autowired
    TbItemDescMapper tbItemDescMapper;
    @Autowired
    TbItemParamItemMapper tbItemParamItemMapper;

    public TbItem selectItemInfo(Long itemId) {
        return tbItemMapper.selectByPrimaryKey(itemId);
    }

    public PageResult selectTbItemAllByPage(String page, String rows) {
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(rows));
       List<TbItem>  list=tbItemMapper.findAll();
        PageInfo<TbItem> pageInfo=new PageInfo<>(list);

        PageResult pageResult = new PageResult();

        pageResult.setResult(pageInfo.getList());
        pageResult.setPageIndex(Integer.parseInt(page));
        pageResult.setTotalPage((long) pageInfo.getPages());
        return pageResult;
    }

    public List<TbItemCat> selectItemCategoryByParentId(Integer id) {
        return itemCatMapper.selectItemCategoryByParentId(id);
    }

    public TbItemParam selectItemParamByItemCatId(Integer itemCatId) {
      return   itemParamMapper.selectItemParamByItemCatId(itemCatId);
    }

    public Integer insertTbItem(TbItem tbItem, String desc, String itemParams) {
        long id = IDUtils.genItemId();
        tbItem.setId(id);
        Date date = new Date();
        tbItem.setCreated(date);
        tbItem.setUpdated(date);
        //逻辑删除 类似与上架和下架的关系
        tbItem.setStatus((byte)1);
        //几条数据被修改了
        int i = tbItemMapper.insertSelective(tbItem);

        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(id);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setCreated(date);
        tbItemDesc.setUpdated(date);
        int i1 = tbItemDescMapper.insertSelective(tbItemDesc);

        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setItemId(id);
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItem.setCreated(date);
        tbItemParamItem.setUpdated(date);
        int i2 = tbItemParamItemMapper.insertSelective(tbItemParamItem);

        return i+i1+i2;
    }

    public Map<String, Object> preUpdateItem(Long itemId) {
        HashMap<String, Object> map = new HashMap<>();
        //商品表的回显
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        map.put("item",tbItem);
        //分类的回显
        TbItemCat tbItemCat = itemCatMapper.selectByPrimaryKey(tbItem.getCid());
        map.put("itemCat",tbItemCat.getName());
        //描述的回显
        TbItemDesc tbItemDesc = tbItemDescMapper.selectByPrimaryKey(itemId);
        map.put("itemDesc",tbItemDesc.getItemDesc());
        //规格参数表
       List<TbItemParamItem> list= tbItemParamItemMapper.selectByItemId(itemId);
        map.put("itemParamItem",list.get(0).getParamData());

        return map;
    }

    public int deleteItemById(Long itemId) {
        int tbItem = tbItemMapper.deleteByPrimaryKey(itemId);
        int tbItemDesc = tbItemDescMapper.deleteByPrimaryKey(itemId);
        TbItemParamItemExample example = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        int tbItemParamItem = tbItemParamItemMapper.deleteByExample(example);
        return tbItem + tbItemDesc + tbItemParamItem;
    }

    public Integer updateTbItem(TbItem tbItem, String desc, String itemParams) {
        Date date=new Date();
        tbItem.setPrice(tbItem.getPrice()*100);
        tbItem.setUpdated(date);
        int i1 = tbItemMapper.updateByPrimaryKeySelective(tbItem);
        TbItemDesc tbItemDesc=new TbItemDesc();
        tbItemDesc.setItemId(tbItem.getId());
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setUpdated(date);
        int i2 = tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);
        TbItemParamItemExample example=new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(tbItem.getId());
        TbItemParamItem tbItemParamItem=new TbItemParamItem();
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItem.setUpdated(date);
        int i3 = tbItemParamItemMapper.updateByExampleSelective(tbItemParamItem, example);
        return i1+i2+i3;
    }
}
